import {
  scrollMixin
} from 'utils/mixin';
export default {
  mixins: [scrollMixin], // 加载更多
  data() {
    return {
      titleTop: ['会员昵称', '本月', '本月', '本年度'], //标题
      titleButton: ['ID','订单数', '订单总额', '订单总额'], //标题
      slideDown: false, //下拉筛选是否显示
      selectedTime: '', //已选中时间
      minDate: new Date(2020, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(),
      newTime: this.dealdate(new Date()),
      teamSalesList: [],
      pluApi: {},

      //more
      isLoadMore: true,
      page: 1,
      total_page: 0,
      pluApiAll: {
        agentEarnings: {
          url: 'plugin.agent-earnings.frontend.sales-statistical.get-sales-detail',
          title: '团队销售额'
        },
        levelDistribution: {
          url: 'plugin.level-distribution.frontend.controllers.income.performance',
          title: '直推客户业绩'
        }
      },
    };
  },
  activated(){
    let plu = this.$route.query.plu||'agentEarnings';
    this.pluApi = this.pluApiAll[plu];
    this.fun.setWXTitle(this.pluApi.title);
    this.getData(this.newTime);
  },
  methods: {
    //获取数据
    getData(month) {
      month = parseInt(month.replace('-', ''));

      $http.post(this.pluApi.url, { month_str: month,agent_level_id: this.$route.query.agent_level_id }).then((res) => {
        if (res.result === 1) {
          let data = res.data;
          this.teamSalesList = data.list?data.list.data: data.data;
          this.isLoadMore = true;
          this.total_page = data.list? data.list.last_page : data.last_page;
          if (!this.total_page) {
            this.total_page = 0;
          }
        } else {
          this.$dialog.alert({
            message: res.msg
          });
        }
      },(response) => {
        // error callback
      });
    },
    //获取更多数据
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        let url = this.pluApi.url;
        $http.get(url,{page: this.page},"加载中").then(
          response => {
            this.isLoadMore = true;
            if (response.result === 1) {
              let myData = response.data?.list?.data||response.data.data;
              this.teamSalesList = this.teamSalesList.concat(myData); //数组拼接
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
            }
          },
          function(response) {
            // error callback
          }
        );
      }
    },
    changeSlideDown(value) { //弹出时间选择弹窗
      if (value) {
        this.selectedTime= this.dealdate(value)
        this.getData(this.selectedTime)
      }
      this.slideDown = !this.slideDown

    },
    cancelTimePopover(){ //时间取消修改
      this.slideDown = false
    },
    screen(value) { //筛选
      this.selectedTime = value
    },
    formatter(type, val) {
      if (type === 'month') {
        return `${val}月`;
      } else if (type === 'day') {
        return `${val}日`;
      }
      return val;
    },
    dealdate(dealdate) { //时间转换
      const date = new Date(dealdate)
      const year = date.getFullYear()
      let month = date.getMonth() + 1
      month = month < 10 ? "0" + month : month
      let day = date.getDate()
      day = day < 10 ? "0" + day : day
      let hour = date.getHours()
      hour = hour < 10 ? "0" + hour : hour
      let minute = date.getMinutes()
      minute = minute < 10 ? "0" + minute : minute
      let second = date.getSeconds()
      second = second < 10 ? "0" + second : second
      return year + '-' + month;
    },
  },
  components: {}
};
